﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using TERP.WaterClient.Common;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;
using TERP.WaterClient.Entities.Auth;
using DevExpress.XtraEditors;
using DevExpress.Utils;

namespace TERP.WaterClient.Views
{
    public partial class FrmMdiBase : Form
    {
        private int iMdiId;

        private bool isExit;

        public FrmMdiBase()
        {
            InitializeComponent();
            this.Icon = EmbeddedRes.Instance.AppIcon;
        }

        public int MdiId
        {
            set { iMdiId = value; }
            get { return iMdiId; }
        }

        public bool IsExit
        {
            set { isExit = value; }
            get { return isExit; }
        }

        #region //Get Value
        protected string GetTextBoxStringValue(TextEdit txtControl)
        {
            string value = txtControl.Text.Trim();
            txtControl.Text = value;
            return value;
        }

        protected string GetDateEditStringValue(DateEdit dateEdit, string format)
        {
            DateTime tmp = dateEdit.DateTime;
            if (tmp == DateTime.MinValue)
                return "";
            return tmp.ToString(format);
        }

        //protected string GetTextBoxValue(TextEdit txtControl)
        //{
        //    string value = Convert.ToString(txtControl.EditValue);
        //    return value;
        //}

        protected long GetTextBoxInt64Value(TextEdit txtControl)
        {
            try
            {
                object value = txtControl.EditValue;
                if (value == null)
                    return 0;
                else
                    return Convert.ToInt64(value);
            }
            catch (Exception)
            {
                return 0;
            }
        }

        protected double GetTextBoxDoubleValue(TextEdit txtControl)
        {
            try
            {
                object value = txtControl.EditValue;
                if (value == null)
                    return 0;
                else
                    return Convert.ToDouble(value);
            }
            catch (Exception)
            {
                return 0;
            }
        }

        protected int GetTextBoxInt32Value(TextEdit txtControl)
        {
            try
            {
                object value = txtControl.EditValue;
                if (value == null)
                    return 0;
                else
                    return Convert.ToInt32(value);
            }
            catch (Exception)
            {
                return 0;
            }
        }

        #endregion //Get Value

        #region //Set Format
        protected void SetGridColumnCurrencyFormat(GridColumn gridCol)
        {
            gridCol.DisplayFormat.FormatType = FormatType.Numeric;
            gridCol.DisplayFormat.FormatString = "n0"; //c0 -> $ +_+
        }

        protected void SetGridColumnNumberFormat(GridColumn gridCol)
        {
            gridCol.DisplayFormat.FormatType = FormatType.Numeric;
            gridCol.DisplayFormat.FormatString = "n0";
        }

        //protected void SetGridColumnMonthFormat(GridColumn gridCol)
        //{
        //    gridCol.DisplayFormat.FormatType = FormatType.DateTime;
        //    gridCol.DisplayFormat.FormatString = Nonsense.MONTH_YEAR_FORMAT;
        //}

        protected void SetGridColumnDateFormat(GridColumn gridCol)
        {
            gridCol.DisplayFormat.FormatType = FormatType.DateTime;
			gridCol.DisplayFormat.FormatString = Nonsense.DATE_FORMAT;
        }

        protected void SetGridColumnTimeFormat(GridColumn gridCol)
        {
            gridCol.DisplayFormat.FormatType = FormatType.DateTime;
            gridCol.DisplayFormat.FormatString = Nonsense.TIME_FORMAT;
        }

        protected void SetGridColumnDateFormatWithHour(GridColumn gridCol)
        {
            gridCol.DisplayFormat.FormatType = FormatType.DateTime;
            gridCol.DisplayFormat.FormatString = Nonsense.DATE_TIME_DB_FORMAT;
        }

        protected void SetReposDateEditControlFormat(DevExpress.XtraEditors.Repository.RepositoryItemDateEdit control)
        {
            control.Mask.UseMaskAsDisplayFormat = true;
            control.Mask.EditMask = Nonsense.DATE_DB_FORMAT;
        }

        protected void SetDateEditControlFormat(DateEdit control)
        {
            control.Properties.Mask.UseMaskAsDisplayFormat = true;
            control.Properties.EditMask = Nonsense.DATE_FORMAT;
        }

        protected void SetDateEditControlFormatWithHour(DateEdit control)
        {
            control.Properties.Mask.UseMaskAsDisplayFormat = true;
            control.Properties.EditMask = Nonsense.DATE_TIME_DB_FORMAT;
        }

        protected void SetGridColumnPercentFormat(GridColumn gridCol)
        {
            gridCol.DisplayFormat.FormatType = FormatType.Numeric;
            gridCol.DisplayFormat.FormatString = "n2"; //p0 -> value in format 0,01 :(
        }

        protected void SetTextBoxFormatNumeric(TextEdit control)
        {
            control.Properties.Mask.UseMaskAsDisplayFormat = true;
            control.Properties.Mask.EditMask = "n0";
            control.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
            control.RightToLeft = RightToLeft.Yes;

        }

        #endregion //Set Format

        #region //Set Button Auth
        protected void SetButtonAuth()
        {
            if (!SystemConfig.Instance.AuthenticateOn)
                return;
            FunctionCollection lstFnc = MasterInit.Instance.ListFunction;
            foreach (Control control in this.Controls)
            {
                if (control is SimpleButton)
                {
                    SimpleButton button = (SimpleButton)control;
                    object tag = button.Tag;
                    if (tag != null && !tag.ToString().Equals(""))
                    {
                        string functionCode = Convert.ToString(tag);
                        if (!lstFnc.ContainsFunctionCode(functionCode))
                            button.Enabled = false;
                    }
                }
            }
        }
        #endregion //Set Button Auth

        #region //Set Style
        protected void SetGridViewStyle(GridView gview)
        {
            //header cua cot grid
            gview.Appearance.HeaderPanel.ForeColor = LayoutStyle.COLOR_GRID_COL_HEADER;
            gview.Appearance.HeaderPanel.Font = LayoutStyle.FONT_BOLD;
            gview.RowStyle += new RowStyleEventHandler(GridView_RowStyle);
            gview.BorderStyle = BorderStyles.NoBorder;
        }

        private void GridView_RowStyle(object sender, RowStyleEventArgs e)
        {
            int handle = e.RowHandle;
            if (handle < 0) return;
            if (handle % 2 != 0)
                e.Appearance.BackColor = LayoutStyle.COLOR_ALTERNAVITE_ROW_BRG;
        }

        protected void gview_DataSourceChanged(object sender, EventArgs e)
        {
            if (sender is GridView)
            {
                this.SetGridViewBestFitColumn((GridView)sender);
            }
        }

        private const int BEST_FIX_MAX_WIDTH = 150;

        protected void SetGridViewBestFitColumn(GridView gview)
        {
            if (gview == null)
                return;
            gview.BestFitColumns();
            foreach (GridColumn gridCol in gview.Columns)
            {
                if (gridCol.Width > BEST_FIX_MAX_WIDTH)
                    gridCol.Width = BEST_FIX_MAX_WIDTH;
            }
        }

        #endregion //Set Style

        #region //Set Index
        protected void SetGridViewIndexNormal(GridView gview)
        {
            gview.IndicatorWidth = IndicatorWidthDefault.NumberIndicatorWidthNormal;
            gview.CustomDrawRowIndicator += new RowIndicatorCustomDrawEventHandler(gview_CustomDrawRowIndicator);
        }

        protected void SetGridViewIndexSmall(GridView gview)
        {
            gview.IndicatorWidth = IndicatorWidthDefault.NumberIndicatorWidthSmall;
            gview.CustomDrawRowIndicator += new RowIndicatorCustomDrawEventHandler(gview_CustomDrawRowIndicator);
        }

        protected void gview_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            GridView view = (GridView)sender;
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }
        #endregion

        #region //Event FormClosing
        protected void SetEventFormClosing()
        {
            this.FormClosing += new FormClosingEventHandler(FrmMdiBase_FormClosing);
        }

        void FrmMdiBase_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (IsExit)
            {
                if (Util.ShowMessageBoxQuestion(InfoMessage.MSG_EXIT_QUESTION))
                {
                    e.Cancel = false;
                }
                else e.Cancel = true;
            }
        }

        #endregion

    }
}
